package com.google.coffeeshop.business;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import com.google.coffeeshop.GeneralConstants;
import com.google.coffeeshop.dao.ConfigurationDAO;
import com.google.coffeeshop.dao.DAOFactory;
import com.google.coffeeshop.dao.DatabaseResource;
import com.google.coffeeshop.dao.InvoiceDAO;
import com.google.coffeeshop.dao.OrderDAO;
import com.google.coffeeshop.dao.SeatDAO;
import com.google.coffeeshop.dao.UserDAO;
import com.google.coffeeshop.entity.ConfigurationDTO;
import com.google.coffeeshop.entity.InvoiceDTO;
import com.google.coffeeshop.entity.OrderDTO;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.entity.UserDTO;

import vn.bang.utility.EncryptionUtil;
import vn.bang.utility.EntityManagerFactoryUtil;
import vn.bang.utility.JpaUtil;

public abstract class AbstractBaseBusiness {
	
	static public EntityManager createEntityManager() {
		final EntityManagerFactory EMF = EntityManagerFactoryUtil.getEntityManagerFactory(DatabaseResource.SERVER_PERSISTENCE_UNIT);
		return JpaUtil.createEntityManager(EMF);
	}
	
	static protected String encryptMD5(final char[] password) {
		return EncryptionUtil.encryptPasswordWithMD5Algorithm(password);
	}
	
	static protected String encryptMD5(final String password) {
		return EncryptionUtil.encryptPasswordWithMD5Algorithm(password);
	}
	
	static protected UserDTO findByUsername(final String username, final EntityManager entityManager) {
		final UserDAO userDAO = DAOFactory.createUserDAO(entityManager);
		return userDAO.findActiveByUsername(username);
	}
	
	static  ConfigurationDTO findConfigByName(final String name, final EntityManager entityManager) {
		ConfigurationDAO configurationDAO = DAOFactory.createConfigurationDAO(entityManager);
		return configurationDAO.findByName(name);
	}
	
	static protected String getConfig(final String name, final EntityManager entityManager) {
		String value = "";
		final ConfigurationDTO configurationDTO = findConfigByName(name, entityManager);
		if (null != configurationDTO) {
			value = configurationDTO.getValue();
		}
		return value;
	}
	
	static protected SeatDTO getSeatById(long seatId, EntityManager entityManager) {
		SeatDAO seatDAO = DAOFactory.createSeatDAO(entityManager);
		SeatDTO seat = seatDAO.find(seatId);
		return seat;
	}
	
	static protected OrderDTO getOrderById(long orderId, EntityManager entityManager) {
		OrderDAO orderDAO = DAOFactory.createOrderDAO(entityManager);
		OrderDTO order = orderDAO.find(orderId);
		return order;
	}
	
	static protected void changeSeatStatus(SeatDTO seat, Integer status,
			EntityManager entityManager) {
		SeatDAO seatDAO = DAOFactory.createSeatDAO(entityManager);
		seat.setStatus(status);
		seatDAO.merge(seat);
	}
	
	static protected void changeSeatStatus(long seatId, Integer status,
			EntityManager entityManager) {
		SeatDAO seatDAO = DAOFactory.createSeatDAO(entityManager);
		SeatDTO seat = seatDAO.find(seatId);
		seat.setStatus(status);
		seatDAO.merge(seat);
	}
	static protected void changeOrderStatus(long orderId, Integer status,
			EntityManager entityManager) {
		OrderDAO orderDAO = DAOFactory.createOrderDAO(entityManager);
		OrderDTO order = orderDAO.find(orderId);
		order.setStatus(status);
		orderDAO.merge(order);
	}
	
	static protected void changeInvoiceStatus(long invoiceId, Integer status,
			EntityManager entityManager) {
		InvoiceDAO invoiceDAO = DAOFactory.createInvoiceDAO(entityManager);
		InvoiceDTO invoice = invoiceDAO.find(invoiceId);
		invoice.setStatus(status);
		invoiceDAO.merge(invoice);
	}
	
	static protected boolean isSeatNew(SeatDTO seat) {
		return GeneralConstants.SEAT_NEW_STATUS.equals(seat.getStatus());
	}
	
	static protected boolean isSeatUsable(SeatDTO seat) {
		return GeneralConstants.SEAT_USABLE_STATUS.equals(seat.getStatus());
	}
	
	static protected boolean isSeatStopped(SeatDTO seat) {
		return GeneralConstants.SEAT_STOP_STATUS.equals(seat.getStatus());
	}
	
	static protected boolean isSeatBusy(SeatDTO seat) {
		return GeneralConstants.SEAT_BUSY_STATUS.equals(seat.getStatus());
	}
	
}
